package com.lanhe.pay.controller;

import cn.hutool.core.util.ObjectUtil;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.order.domain.LhOrdersLevel;
import com.lanhe.order.service.ILhOrdersLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付
 *
 * @author zhanggl
 * @date 2024/1/5
 */
@Slf4j
@RestController
@RequestMapping("/api/wxPay")
public class WxPayController extends BaseController {

	@Autowired
	private ILhOrdersLevelService ordersLevelService;

	/**
	 * 下单
	 *
	 * @param level 实体
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@PostMapping("/pay")
	public AjaxResult pay(@RequestBody LhOrdersLevel level) {
		if (ObjectUtil.isNull(level.getLevelId())) {
			return error("请选择要购买的VIP产品");
		}
		if (ObjectUtil.isNull(level.getOrderType())) {
			return error("请标识订单类型");
		}
		level.setMemberId(getUserId());
		return success(ordersLevelService.insertOrdersLevel(level));
	}


	@PostMapping("/zidingyi/pay")
	public AjaxResult zidingyiPay(@RequestBody LhOrdersLevel level) {
		level.setMemberId(getUserId());
		return success(ordersLevelService.insertZidingyiOrdersLevel(level));
	}



	/**
	 * 下单-积分方式
	 *
	 * @param level 实体
	 * @author zhanggl
	 * @date 2024年01月17日20:24:14
	 */
	@PostMapping("/score")
	public AjaxResult score(@RequestBody LhOrdersLevel level) {
		if (ObjectUtil.isNull(level.getLevelId())) {
			return error("请选择要购买的VIP产品");
		}
		if (ObjectUtil.isNull(level.getOrderType())) {
			return error("请标识订单类型");
		}
		level.setMemberId(getUserId());
		return success(ordersLevelService.insertOrdersLevelByScore(level));
	}

	/**
	 * 支付回调
	 *
	 * @param notifyData 返回数据
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@PostMapping(value="notify")
	public  Map<String, String> notify(@RequestBody String notifyData) {

		log.info("微信支付回调数据:{}", notifyData);
		ordersLevelService.notifyOrdersLeve(notifyData);
		Map<String, String> res = new HashMap<>();
		res.put("code", "SUCCESS");
		res.put("message", "成功");
		return res;
	}
}


